<?php
namespace app\api\controller;

use think\Request;

class History extends BaseController
{
    public function ls ()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $user_id = input('post.user_id');
            $room_id = input('post.room_id');
            $per_page = 20;
            $page = input('post.page');

            $check_in_model = model('CheckIn');

            $where = [
                'user_id' => $user_id,
                'room_id' => $room_id,
                'is_deleted' => '0'
            ];

            $data['history'] = $check_in_model->where($where)->limit(($page - 1) * $per_page, $per_page)->select();

            foreach($data['history'] as $key => $history) {
                if ($history['check_in_date'] && $history['check_out_date']) {
                    $data['history'][$key]['check_in_date'] = date('Y-m-d', $history['check_in_date']);
                    $data['history'][$key]['check_out_date'] = date('Y-m-d', $history['check_out_date']);
                    $data['history'][$key]['check_in_status'] = '1';
                } else {
                    if ($history['check_in_date'] > strtotime(date('Y-m-d'))) {
                        $data['history'][$key]['check_in_status'] = '0';
                        $data['history'][$key]['check_in_status_name'] = '未入住';
                    } else {
                        $data['history'][$key]['check_in_date'] = date('Y-m-d', $history['check_in_date']);
                        $data['history'][$key]['check_in_status'] = '2';
                    }
                }
            }
            $data['status'] = 200;
        }

        return json($this->dataReturn($data));
    }
}